package member

import (
	"context"

	"group-buy/group_buy/internal/models"
	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AllMemberLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAllMemberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AllMemberLogic {
	return &AllMemberLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AllMemberLogic) AllMember(req *types.AllMemberReq) (resp *types.AllMemberRsp, err error) {
	data := make([]*models.Member, 0)
	sql := l.svcCtx.Engine.Where("")
	if req.Role > 0 {
		sql = sql.And("role=?", req.Role)
	}
	err = sql.Find(&data)
	list := make([]*types.Member, len(data), len(data))
	for i, _ := range data {
		list[i] = &types.Member{
			Id:         data[i].Id,
			Name:       data[i].Name,
			Phone:      data[i].Phone,
			Cover:      data[i].Cover,
			OpenId:     data[i].OpenId,
			LsUser:     data[i].LsUser,
			LsCard:     data[i].LsCard,
			Role:       data[i].Role,
			BusinessId: data[i].BusinessId,
		}
	}
	return &types.AllMemberRsp{List: list}, nil
}
